Kameleon-Plus  0.3.2
Vector.h
Go to the documentation of this file.
1 /*
2  * Vector.h
3  *
4  * Created on: Apr 21, 2009
5  * Author: David Berrios
6  */
7 
8 #ifndef VECTOR_H_
9 #define VECTOR_H_
10 
11 #include <iostream>
12 #include <sstream>
13 #include <cmath>
14 #include "Point.h"
15 
16 namespace ccmc
17 {
18  /*
19  *
20  */
21  template<class T>
22  class Vector: public Point<T>
23  {
24  public:
25  Vector();
26  Vector(T c0, T c1, T c2);
27  T length();
28  Vector<T> operator+(const Vector<T>& v) const;
29  Vector<T> operator-(const Vector<T>& v) const;
30  Vector<T> operator*(const T& value) const;
31  Vector<T> operator/(const T& value) const;
32  void operator+=(const Vector<T>& v);
33  void operator-=(const Vector<T>& v);
34  void operator*=(const T& value);
35  void operator/=(const T& value);
36  static T dot(Vector<T>& a, Vector<T>& b);
37  static T dot(Vector<T>* a, Vector<T>* b);
38  static Vector<T> cross(Vector<T>& a, Vector<T>& b);
39  static Vector<T> cross(Vector<T>* a, Vector<T>* b);
40  static void cross(Vector<T>& c, Vector<T>& a, Vector<T>& b);
41  static T triple(Vector<T>& a,Vector<T>& b,Vector<T>& c);
42  static Vector<T> norm(Vector<T>& a);
43  static Vector<T> norm(Vector<T>* a);
44  void norm();
45  static T angle(Vector<T>& a, Vector<T>& b);
46  static void angle(T& angle, Vector<T>& a, Vector<T>& b);
47 
48  static Vector<T> dummy;
49 
50  virtual ~Vector();
51  };
52 
53  template<class T>
55  Point<T> ()
56  {
57 
58  }
59 
60  template<class T>
62 
63  template<class T>
65  {
66 
67  }
68 
69  template<class T>
70  Vector<T>::Vector(T c0, T c1, T c2) :
71  Point<T> (c0, c1, c2)
72  {
73 
74  }
75 
76  template<class T>
78  {
79  T c0 = this->components[0];
80  T c1 = this->components[1];
81  T c2 = this->components[2];
82  return sqrt(c0 * c0 + c1 * c1 + c2 * c2);
83  }
84 
85  template<class T>
87  {
88  return a.c0() * b.c0() + a.c1() * b.c1() + a.c2() * b.c2();
89  }
90 
91  template<class T>
93  {
94  return a->c0() * b->c0() + a->c1() * b->c1() + a->c2() * b->c2();
95  }
96 
97  template<class T>
99  {
100  return Vector<T> (a.c1() * b.c2() - a.c2() * b.c1(), a.c2() * b.c0() - a.c0() * b.c2(), a.c0() * b.c1()
101  - a.c1() * b.c0());
102  }
103 
104  template<class T>
106  {
107  return Vector<T> (a->c1() * b->c2() - a->c2() * b->c1(), a->c2() * b->c0() - a->c0() * b->c2(), a->c0()
108  * b->c1() - a->c1() * b->c0());
109  }
110 
111  template<class T>
112  void Vector<T>::cross(Vector<T>& c, Vector<T> &a, Vector<T> &b) //should be faster than cross(a,b)
113  { // c = a cross b
114  c.components[0] = a.c1() * b.c2() - a.c2() * b.c1();
115  c.components[1] = a.c2() * b.c0() - a.c0() * b.c2();
116  c.components[2] = a.c0() * b.c1() - a.c1() * b.c0();
117  }
118 
119  template<class T>
121  Vector<T> temp = cross(b,c);
122  return dot(a,temp);
123  }
124 
125  template<class T>
127  {
128  return T(atan2(Vector<T>::cross(a,b).length(),Vector<T>::dot(a,b)));
129  }
130 
131  template<class T>
132  void Vector<T>::angle(T& angle, Vector<T>& a, Vector<T>& b) //should be faster than angle(a,b)
133  {
134  Vector<T>::cross(Vector<T>::dummy, a, b); //dummy stores cross product of a and b
135  angle = atan2(Vector<T>::dummy.length(), Vector<T>::dot(a,b));
136  return;
137  }
138 
139  template<class T>
141  {
142  T length = a.length();
143  return Vector<T> (a.c0() / length, a.c1() / length, a.c2()/length);
144  }
145 
146  template<class T>
148  {
149  T length = a->length();
150  return Vector<T> (a->c0() / length, a->c1() / length, a->c2()/length);
151  }
152 
153  template<class T>
155  {
156  //call the overloaded /= operator and normalize the components of this object
157  (*this)/=this->length();
158  }
159 
160  template<class T>
162  {
163  return Vector<T> (this->components[0]+v.components[0],this->components[1]+v.components[1],
164  this->components[2]+v.components[2]);
165  }
166 
167  template<class T>
169  {
170  this->components[0]+=v.components[0];
171  this->components[1]+=v.components[1];
172  this->components[2]+=v.components[2];
173  }
174 
175  template<class T>
177  {
178  return Vector<T> (this->components[0]-v.components[0],this->components[1]-v.components[1],
179  this->components[2]-v.components[2]);
180  }
181 
182  template<class T>
184  {
185  this->components[0]-=v.components[0];
186  this->components[1]-=v.components[1];
187  this->components[2]-=v.components[2];
188  }
189 
190  template<class T>
191  Vector<T> Vector<T>::operator*(const T& value) const
192  {
193  return Vector<T> (this->components[0]*value, this->components[1]*value,this->components[2]*value);
194 
195  }
196 
197  template<class T>
198  void Vector<T>::operator*=(const T& value)
199  {
200  this->components[0]*=value;
201  this->components[1]*=value;
202  this->components[2]*=value;
203  }
204 
205  template<class T>
206  Vector<T> Vector<T>::operator/(const T& value) const
207  {
208  return Vector<T> (this->components[0]/value, this->components[1]/value, this->components[2]/value);
209  }
210 
211  template<class T>
212  void Vector<T>::operator/=(const T& value)
213  {
214  this->components[0]/=value;
215  this->components[1]/=value;
216  this->components[2]/=value;
217  }
218 
219 }
220 
221 #endif /* VECTOR_H_ */